/* 
 * File:   TestModules.cpp
 * Author: pkv
 *
 * Created on July 24, 2011, 12:20 PM
 */

#include "core/ModuleLoader.h"
#include "core/Filesystem.h"
#include "FakeStaticModule.h"

#include <iostream>
#include <string>

using namespace std;

/*
 * Runs a test to check if dynamic module loading is working correctly.  It 
 * attempts to load the test module FakeModule.
 */
int main(int argc, char** argv) {

    ModuleLoader loader;
    
    cout << "Checking that module list starts empty...";
    if (!loader.list().empty())
    {
        cerr << "Module list not initially empty." << endl;
        return 1;
    }
    cout << "success." << endl;
    
    cout << "Loading static module...";
    if (!loader.load<Module::FakeStaticModule1>())
    {
        cerr << "Unable to load static module 1." << endl;
        return 1;
    }
    cout << "success." << endl;
    
    cout << "Verifying that static module was added to list...";
    if ((*(loader.list().begin())).compare("FakeStaticModule1") != 0)
    {
        cerr << "Unable to find ststic module in list." << endl;
        return 1;
    }
    cout << "success." << endl;
    
    cout << "Loading another static module...";
    if (!loader.load<Module::FakeStaticModule2>())
    {
        cerr << "Unable to load static module 2." << endl;
        return 1;
    }
    cout << "success." << endl;
    
    cout << "Unloading static module by template...";
    if (!loader.unload<Module::FakeStaticModule1>())
    {
        cerr << "Unable to unload static module." << endl;
        return 1;
    }
    cout << "success." << endl;
    
    cout << "Unloading static module by name...";
    if (!loader.unload("FakeStaticModule2"))
    {
        cerr << "Unable to unload static module." << endl;
        return 1;
    }
    cout << "success." << endl;
    
    cout << "Loading shared module...";
    if (!loader.load("FakeModule"))
    {
        cerr << "Unable to load shared module." << endl;
        return 1;
    }
    cout << "success." << endl;
    
    cout << "Verifying that shared module was added to list...";
    if ((*(loader.list().begin())).compare("FakeModule") != 0)
    {
        cerr << "Unable to find test module in list." << endl;
        return 1;
    }
    cout << "success." << endl;
    
    cout << "Unloading shared module...";
    if (!loader.unload("FakeModule"))
    {
        cerr << "Unable to unload test module." << endl;
        return 1;
    }
    cout << "success." << endl;
    
    cout << "Verifying that test module was removed from list...";
    if (!loader.list().empty())
    {
        cerr << "Module list not empty after unloading." << endl;
        return 1;
    }
    cout << "success." << endl;
    
    cout << "Loading shared module from archive...";
    Filesystem fs;
    fs.mount("01_ModuleLoading_Payload.zip", "test");
    if (!loader.load("FakeModule2", NULL, "test"))
    {
        cerr << "Unable to load shared module." << endl;
        return 1;
    }
    cout << "success." << endl;
    
    cout << "Verifying that shared module was added to list...";
    if ((*(loader.list().begin())).compare("FakeModule2") != 0)
    {
        cerr << "Unable to find test module in list." << endl;
        return 1;
    }
    cout << "success." << endl;
    
    cout << "Unloading shared module from archive...";
    if (!loader.unload("FakeModule2"))
    {
        cerr << "Unable to unload test module." << endl;
        return 1;
    }
    cout << "success." << endl;
    
    return 0;
}

